import { CUSTOMER_GROUP_KEY } from '@/app/hooks/dictKey'
import { useDictLoading } from '@/app/hooks/useDict'
import { useSaleTabStore } from '../../store/useSaleTabStore'
import { useMemo, useEffect } from 'react'

export const useSaleTab = () => {
  const [options, dictLoading] = useDictLoading(CUSTOMER_GROUP_KEY)
  const setActive = useSaleTabStore((s) => s.setActive)
  const activeTab = useSaleTabStore((s) => s.active)

  const tabsVisible = useMemo(() => {
    const tabs = options?.map((i, index) => ({
      label: i.label,
      key: i.value,
      index,
      isOther: index == options.length - 1,
    }))
    return tabs
  }, [options])

  useEffect(() => {
    if (tabsVisible && tabsVisible.length > 0 && !activeTab?.key) {
      setActive(tabsVisible[0])
    }
  }, [tabsVisible, activeTab?.key])
  return [tabsVisible, dictLoading] as const
}
